package ro.prjmanager.core.context;

import net.sourceforge.stripes.action.ActionBeanContext;
import ro.prjmanager.core.usr.UserSession;


@SuppressWarnings({"unchecked"})
public class SecurityContext<U,P,S,D,SC>  extends ActionBeanContext{
    public static final String USER_KEY = "user";

    public UserSession<U,P,S,D,SC> getUserSession(){
       if(getRequest().getSession().getAttribute(USER_KEY)!=null){
           return (UserSession<U,P,S,D,SC>) getRequest().getSession().getAttribute(USER_KEY);
       }
        return null;
    }

    public void setUserSession(UserSession userSession){
        getRequest().getSession().setAttribute(USER_KEY,userSession);
    }
    public U getUser(){
        UserSession<U,P,S,D,SC> usrSession=getUserSession();
        if(usrSession!=null)
            return usrSession.getUser();
        return null;
    }

    public void logout(){
        getRequest().getSession().invalidate();
    }
}
